package com.baizhi.controller;


import com.baizhi.entity.CmfzGuru;
import com.baizhi.service.GuruService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.*;

@Controller
@RequestMapping("guru")
public class GuruController {

    @Autowired
    private GuruService guruService;

    @RequestMapping("selectAll")
    @ResponseBody
    public Map selectAll(String name,Integer page, Integer limit){
        Page<CmfzGuru> cmfzGuruPage = guruService.selectAll(name,page, limit);
        Map map = new HashMap<>();
        map.put("code",0);
        map.put("msg",0);
        map.put("count",cmfzGuruPage.getTotal());
        map.put("data",cmfzGuruPage.getRecords());
        return map;
    }
    @RequestMapping("upload")
    @ResponseBody
    public Map upload(MultipartFile file) throws Exception{
        String  newName= UUID.randomUUID().toString()+file.getOriginalFilename();
        file.transferTo(new File("E:\\upload",newName));
        Map map = new HashMap();
        map.put("newName",newName);
        return map;
    }
    @RequestMapping("insert")
    @ResponseBody
    public  Map   insert(CmfzGuru cmfzGuru){
        Map map = new HashMap<>();
        try {
            guruService.insert(cmfzGuru);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }

   @RequestMapping("delete")
    @ResponseBody
    public  Map   delete(Integer id){
        Map map = new HashMap<>();
        try {
            guruService.delete(id);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("selectOne")
    @ResponseBody
    public  CmfzGuru   selectOne(Integer id){
        return guruService.showOne(id);
    }


    @RequestMapping("update")
    @ResponseBody
    public  Map  update(CmfzGuru cmfzGuru){
        Map map = new HashMap<>();
        try {
            guruService.update(cmfzGuru);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("deleteAll")
    @ResponseBody
    public  Map  deleteAll(List<CmfzGuru> ids){
        Map map = new HashMap<>();
        try {
            guruService.deleteAll(ids);
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("/exportExcel")

    public void exportExcel(HttpServletResponse resp){
        HSSFWorkbook workbook = guruService.getGurusWorkbook();
//        文件保存本地
        try {
            // 设置 response
            resp.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode("guru.xls","utf-8"));
            resp.setContentType("application/vnd.ms-excel");
            workbook.write(resp.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("importExcel")
    @ResponseBody
    public Map importExcel (MultipartFile file)  {
        Map map = new HashMap<>();
        System.out.println(file.getOriginalFilename());
        try {
            InputStream inputStream = file.getInputStream();
            HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
//            3.从文件中获取对象 getSheetAt通过下标获取
            HSSFSheet sheet = workbook.getSheetAt(0);
//        4.从表中获取到行数据  从第二行开始 到 最后一行  getLastRowNum()  获取最后一行的下标
            int lastRowNum = sheet.getLastRowNum();

            for (int i = 1; i <=lastRowNum ; i++) {
//            通过下标获取行
                HSSFRow row = sheet.getRow(i);

//            从行中获取数据
                HSSFCell cell = row.getCell(0);

                int id= Integer.parseInt(cell.getStringCellValue());

                String name = row.getCell(1).getStringCellValue();

                String img = row.getCell(2).getStringCellValue();

                String nickname = row.getCell(3).getStringCellValue();

                int status = Integer.parseInt(row.getCell(4).getStringCellValue());

                CmfzGuru cmfzGuru = new CmfzGuru();
                cmfzGuru.setGuruId(id);
                cmfzGuru.setGuruImage(img);
                cmfzGuru.setGuruName(name);
                cmfzGuru.setGuruNickname(nickname);
                cmfzGuru.setGuruStatus(status);
                System.out.println(cmfzGuru);
                //将数据添加到数据库
                guruService.insert(cmfzGuru);
                map.put("flag",true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);

        }
        return map;
    }


}
